#include "gameview.h"

#include <QDebug>

GameView::GameView(QWidget *parent) :
    QWidget(parent)
{
    celW = celH = 30; //每一格的长宽
    row = 15;   //行列书
    cursorX = cursorY = celW*7 - celW/2; //初始化游戏光标的坐标
    gameAraeH = gameAraeW = celH*row+celW; //设置游戏区域的长宽

    controller = new GameControl(celW, celH, row);
    gameAera = new QPixmap(gameAraeW, gameAraeH);
    tmpGameAera = new QPixmap(gameAraeW, gameAraeH);
    time = new QTimer(this);

    gameAera->fill("#F7E2A2");
    connect(time, SIGNAL(timeout()), this, SLOT(computerMove()));
    time->start(500); //设置时间间隔

    this->initGame();
}

//初始化游戏界面
void GameView::initGame()
{
    setCursor(Qt::PointingHandCursor);
    drawCel();
    *tmpGameAera = *gameAera;
}

//重新开始游戏
void GameView::reInit()
{
    QPixmap *_gameAera = gameAera;
    QPixmap *_tmpGameAera = tmpGameAera;
    gameAera = new QPixmap(gameAraeW, gameAraeH);
    tmpGameAera = new QPixmap(gameAraeW, gameAraeH);
    gameAera->fill("#F7E2A2");

    controller->initData();
    time->start();

    delete _gameAera;
    delete _tmpGameAera;

    initGame();
}

//设置游戏开始
void GameView::setPlaying(bool b)
{
    controller->setPlaying(b);
}

//画图主函数
void GameView::paintEvent(QPaintEvent *)
{
    QPainter painter(this);
    painter.drawPixmap(QPoint(0, 0), *tmpGameAera);
    *tmpGameAera = *gameAera;
}

//监听鼠标点击事件
void GameView::mousePressEvent(QMouseEvent *event)
{

    if (event->button() == Qt::LeftButton)
    {
        QPoint p = event->pos();
        if (p.x() >= celW && p.x() <= celW*row && p.y() >= celH
                && p.y() <= celH*row && controller->getIsPlayer() && controller->getIsPlaying())
        {
            QPoint newP = calculateXY(p);
            if (!controller->isHaveChess(QPoint(p.x(), p.y())))
            {

                drawChess(newP.x(), newP.y());

                if (controller->isPlayerWin())
                {
                    time->stop();
                }

                controller->setPlayer(false);
            }
        }
    }
}

//监听键盘事件
void GameView::keyPressEvent(QKeyEvent *event)
{
    switch (event->key())
    {
    case Qt::Key_Left:
        if (cursorX > celW)
        {
            cursorX -= celW;
        }
        break;
    case Qt::Key_Right:
        if (cursorX <= celW*(row-1))
        {
            cursorX += celW;
        }
        break;
    case Qt::Key_Up:
        if (cursorY > celH)
        {
            cursorY -= celH;
        }
        break;
    case Qt::Key_Down:
        if (cursorY <= celH*(row-1))
        {
            cursorY += celH;
        }
        break;
    case Qt::Key_Space:
        if (controller->getIsPlayer() && controller->getIsPlaying())
        {
            if (!controller->isHaveChess(QPoint(cursorX, cursorY)))
            {
                drawChess(cursorX,cursorY);
            }

            if (controller->isPlayerWin())
            {
                time->stop();
            }

            controller->setPlayer(false);
        }

        break;
    }

    drawCursor(QPoint(cursorX, cursorY));
    update();
}

//画网格
void GameView::drawCel()
{
    QPainter painter(gameAera);
    painter.setPen(Qt::SolidLine);

    for (int i = 1; i <= row; i++)
    {
        if (i == row/2)
        {
            painter.fillRect((i+1)*celW-3, (i+1)*celH-3, 6, 6, Qt::black);
        }
        painter.drawLine(QPoint(celW, i*celH), QPoint(celW*row, i*celH));
        painter.drawLine(QPoint(i*celW, celH), QPoint(i*celW, celH*row));
    }
}

//画棋子
void GameView::drawChess(int x, int y)
{
    QPainter painter(gameAera);
    QPainter tmpPainter(tmpGameAera);

    QPixmap pixMap;
    if (controller->getIsPlayer())
    {
        pixMap.load(":/img/black.png");
    }
    else
    {
        pixMap.load(":/img/white.png");
    }
    painter.drawPixmap(x, y, 30, 30, pixMap);
    tmpPainter.drawPixmap(x, y, 30, 30, pixMap);
    update();
}

//画光标
void GameView::drawCursor(QPoint p)
{
    int x = p.x();
    int y = p.y();
    QPainter painter(tmpGameAera);

    painter.setPen(Qt::CustomDashLine);
    painter.drawRect(x, y, celW, celH);
}

//计算画棋子的位置
QPoint GameView::calculateXY(QPoint p)
{
    QPoint newP;
    int x = p.x();
    int y = p.y();
    int x_= x%celW;
    int y_ = y%celH;

    if (x_ >= 15)
    {
        x = x - x_+celW;
    }
    else
    {
        x = x - x_;
    }

    if (y_ >= 15)
    {
        y = y - y_+celH;
    }
    else
    {
        y = y - y_;
    }

    newP.setX(x-15);
    newP.setY(y-15);

    return newP;
}

//电脑落子
void GameView::computerMove()
{
    if (!controller->getIsPlayer() && controller->getIsPlaying())
    {
        QPoint p = controller->computerMove();

        drawChess(p.x(), p.y());

        if (controller->isComputerWin())
        {
            time->stop();
        }

        controller->setPlayer(true);
    }
}
